package co.edu.usbcali.banco.dataaccess.dao;

import co.edu.usbcali.banco.dataaccess.sessionFactory.HibernateSessionFactory;
import co.edu.usbcali.banco.modelo.Estado;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;

import org.hibernate.criterion.Example;

import java.math.BigDecimal;

import java.util.Date;
import java.util.List;
import java.util.Set;


/**
 * A data access object (DAO) providing persistence and search support for
 * Estado entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 *
 * @author Zathura Code Generator http://code.google.com/p/zathura
 *
 */
public class EstadoDAO implements IEstadoDAO {
    private static final Log log = LogFactory.getLog(EstadoDAO.class);

    // property constants
    //public static final String  CODIGOESTADO = "codigoestado";
    public static final String CODIGOESTADO = "codigoestado";

    //public static final String  NOMBREESTADO = "nombreestado";
    public static final String NOMBREESTADO = "nombreestado";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            Estado Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(Estado instance) {
        log.debug("saving Estado instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            Estado Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(Estado instance) {
        log.debug("deleting Estado instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            Estado Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(Estado instance) {
        log.debug("updating Estado instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public Estado findById(String id) {
        log.debug("finding Estado instance with id: " + id);

        try {
            Estado instance = (Estado) getSession().get(Estado.class, id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding Estado failed", re);
            throw re;
        }
    }

    public List<Estado> findByExample(Estado instance) {
        log.debug("finding Estado instance by example");

        try {
            List results = getSession().createCriteria("Estado")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  Estado entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< Estado> found by query
        */
    public List<Estado> findByProperty(String propertyName, Object value) {
        log.debug("finding Estado instance with property: " + propertyName +
            ", value: " + value);

        try {
            String queryString = "from Estado as model where model." +
                propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<Estado> findByCodigoestado(Object codigoestado) {
        return findByProperty(CODIGOESTADO, codigoestado);
    }

    public List<Estado> findByNombreestado(Object nombreestado) {
        return findByProperty(NOMBREESTADO, nombreestado);
    }

    /**
    * Find all Estado entities.
    *
    * @return List<Estado> all Estado instances
    */
    public List<Estado> findAll() {
        log.debug("finding all Estado instances");

        try {
            String queryString = "from Estado";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<Estado> findByCriteria(String whereCondition) {
        log.debug("finding Estado " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from Estado model " +
                where;
            Query query = getSession().createQuery(queryString);
            List<Estado> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in Estado failed", re);
            throw re;
        }
    }

    public List<Estado> findPageEstado(String sortColumnName,
        boolean sortAscending, int startRow, int maxResults) {
        log.debug("finding Estado findPageEstado");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from Estado model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from Estado model";

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberEstado() {
        log.debug("finding Estado count");

        try {
            String queryString = "select count(*) from Estado model";
            Query queryObject = getSession().createQuery(queryString);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
}
